<?php

class News_Page_Main_Category_View_Exception extends News_Page_Main_Category_Exception {}
class News_Page_Main_Category_View_Invalid_Exception extends News_Page_Main_Category_View_Exception {}

class News_Page_Main_Category_View extends News_Page_Main_Category {

	protected function URL($array = array()){
		return parent::URL(array_merge(
			array('view'),
			$array
		));
	}

	public function __construct($args){
		parent::__construct($args);

		$this->category = News_Category::GetByID($args['id']);

		$articles = News_Article::GetByCategory($this->category->id());

		$this->articles = array();

		foreach($articles as $article){
			$this->articles[$article->date()][$article->id()] = $article;
		}

		krsort($this->articles);

	}

	public static function Load($args){
		return new News_Page_Main_Category_View($args);
	}

	public function run(){
		parent::run();
	}

	public function output($child = null){
		$language = Authentication::getInstance()->user()->language();
		$template = Output::getInstance()->Template(array('news','page','category','view'));
		$template->articles = array();
		$template->title = $this->category->name();
		foreach($this->articles as $date){
			foreach($date as $id){
				$template->articles[] = array(
					'title' => $id->title(),
					'body' => $id->body(),
					'date' => date($language->translate(array('news','article','dateformat')),$id->date())
				);
			}
		}
		return parent::output($template);
	}

}
